package com.lnj.community.utiles;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.lnj.community.config.OSSConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OSSUtile {
    public static String uploadToAliyun(InputStream inputStream, String fileName) {
        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(OSSConfig.ENDPOINT, OSSConfig.ACCESS_KEY_ID, OSSConfig.ACCESS_KEY_SECRET);

        try {
            // 创建PutObjectRequest对象
            PutObjectRequest putObjectRequest = new PutObjectRequest(OSSConfig.BUCKET_NAME, fileName, inputStream);

            // 上传文件
            ossClient.putObject(putObjectRequest);

            // 根据阿里云 OSS 的 URL 格式生成文件的 URL
            return "https://" + OSSConfig.BUCKET_NAME + "." + OSSConfig.ENDPOINT + "/" + fileName;
        } finally {
            // 关闭OSSClient
            ossClient.shutdown();
        }
    }
    //从阿里云上面删除图片
    public static void deleteFromAliyun(String fileName) {
        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(OSSConfig.ENDPOINT, OSSConfig.ACCESS_KEY_ID, OSSConfig.ACCESS_KEY_SECRET);
        try {
            // 删除文件
            ossClient.deleteObject(OSSConfig.BUCKET_NAME, fileName);
        } finally {
            // 关闭OSSClient
            ossClient.shutdown();
        }
    }

}
